﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SCFHS.Data;
namespace SCFHS.Views
{
    public partial class Login : System.Web.UI.Page
    {

        SCFHS.Code.Controller controller = new SCFHS.Code.Controller();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Data.User user = controller.GetCredentials(txtUserName.Text);
            if (user == null)
            {
                lblLoginMSG.Text = "عفوا لا يوجد مستخدم بهذا الإسم";
                return;
            }
            if (!user.isActive)
            {
                lblLoginMSG.Text = "المستخدم غير مصرح له بإستخدام التطبيق";
                return;
            }

            if (string.Compare(user.userPassword, txtPassword.Text) == 0)
            {
                string userData = user.userID.ToString() + "|" + user.userName + "|" + user.userProfileName + "|" + user.userDefaultPage + "|" + Convert.ToInt16(user.isAdmin).ToString() + "|" + user.userRole;
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.userName, DateTime.Now, DateTime.Now.AddMinutes(20), ckbxRemmeberMe.Checked, userData);
                string loginCookie = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, loginCookie);
                cookie.Expires = ticket.Expiration;
                cookie.Path = FormsAuthentication.FormsCookiePath;
                Response.Cookies.Add(cookie);

                string strRedirect;
                strRedirect = Request["ReturnUrl"];
                if (strRedirect == null)
                    strRedirect = "default.aspx";
                Response.Redirect(strRedirect, true);

                //Session.Add(controller.userSession, user);
                //FormsAuthentication.RedirectFromLoginPage(user.userProfileName, ckbxRemmeberMe.Checked);
            }
            else
                lblLoginMSG.Text = "عفواً رقم المرور  غير صحيح";
        }
    }
}